package com.djf.eg.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.djf.eg.entity.UserInfo;
import com.djf.eg.result.Result;
import com.djf.eg.result.ResultCode;
import com.djf.eg.service.AuthService;
import com.djf.eg.service.UserService;
import com.djf.eg.vo.UserInfoVo;

@RestController
@RequestMapping("/auth")
public class AuthController extends DBaseController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private AuthService authService;
	
	@PostMapping(value = "/login")
	//Map
	public Result login(
			@RequestBody UserInfo user) {
		Map<String, String> result = new HashMap<String, String>();
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
        try {
            subject.login(token);
//            result.put("principal", JSON);
            result.put("token", subject.getSession().getId().toString());
            result.put("status", "success");
        } catch (AuthenticationException e) {
//        	throw new DSecurityException(MessageConstants.CODE_AUTH_INVALID_PASSWORD, MessageConstants.MESSAGE_AUTH_INVALID_PASSWORD);
        	return Result.failure(ResultCode.USER_LOGIN_ERROR);
//            e.printStackTrace();
//            result.put("status", "failed");
//            result.put("message", e.getCause()==null?e.getMessage():e.getCause().getMessage());
        }
		return Result.success(result);
    }
	
	@PostMapping(value = "/register")
	public Map<String, String> register(@RequestBody UserInfo user) {
		Map<String, String> result = new HashMap<String, String>();
		UserInfo userInfo = userService.register(user.getUsername(), user.getPassword());
//		boolean result = false;
		if (userInfo != null) {
//			result = true;
			result.put("status", "success");
		} else {
			result.put("status", "failed");
			result.put("message", "注册失败，请联系管理员");
		}
        return result;
	}
	
	@PostMapping("/getInfo")
	public Result getInfo() {
		
		UserInfoVo userInfoVo = authService.getInfo(); 
		if (userInfoVo == null) {
//			throw new DNoDataFoundException(MessageConstants.CODE_NO_DATA_FOUND, MessageConstants.MESSAGE_NO_DATA_FOUND);
			return Result.failure(ResultCode.RESULE_DATA_NONE);
		} else {
//			return Result.success(userInfoVo);
			return Result.success(userInfoVo);
		}
	}
	
	@PostMapping("/logout")
	public Result logout() {
		if (authService.logout()) {
			return Result.success();
		} else {
//			throw new DSecurityException(MessageConstants.CODE_AUTH_LOGOUT_ERROR, MessageConstants.MESSAGE_AUTH_LOGOUT_ERROR);
			return Result.failure(ResultCode.USER_LOGOUT_ERROR);
		}
	}
}

